Skip to content

facet_wrap(drop = TRUE) can handle character variables with NAs #5847

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 29, 2024

Conversation

teunbrand
Copy link
Collaborator

This PR aims to fix #5485.

Briefly, ulevels() was always dropping NAs, leading to data being mapped to non-existing PANELs and generating invalid scale_ids. By including NAs, we prevent these invalid scale_ids.

stat_summary_2d() was also making use of ulevels() where NAs should be dropped. Therefore, ulevels() gets an na.last argument that is propagated to sort() so we can pick when to drop NA and when not.

Reprex from issue:

devtools::load_all("~/packages/ggplot2")
#> ℹ Loading ggplot2
library(palmerpenguins)

penguins |>
  transform(sex = as.character(sex)) |>
  ggplot() +
  geom_point(aes(x = body_mass_g, y = flipper_length_mm)) +
  facet_wrap(~sex, drop = FALSE)
#> Warning: Removed 2 rows containing missing values or values outside the scale range
#> (`geom_point()`).

Created on 2024-04-18 with reprex v2.1.0

Copy link
Member

@thomasp85 thomasp85 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@teunbrand teunbrand merged commit c5a9c68 into tidyverse:main Apr 29, 2024
7 of 12 checks passed
@teunbrand teunbrand deleted the facet_character_drop branch April 29, 2024 09:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: where facet variable is character & drop = F
2 participants